package com.example.my.servlet;

import com.example.my.bean.news;
import com.example.my.dao.NewsDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;

@WebServlet("/newsEdit.jsp")
public class NewsEditServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        String newsId = req.getParameter("newsId");
        String newsTitle = req.getParameter("upd_NewsTitle");
        String newsContent = req.getParameter("upd_NewsContent");
        newsBean news = new newsBean();
        //设置新闻对象属性值
        news.setName(Integer.parseInt(newsId));//强制转换
        news.setNameTitle(newsTitle);
        news.setNameContent(newsContent);
        news.setNameTime( (new Date()).toString() );
        news.setAdminName("admin");
        NewsDao newsDao = new NewsDao();
        int result = newsDao.update(news);

        if (result > 0) {
            //补充知识 跳转到网页news.html中
            resp.sendRedirect("news.html");//使用结果对象 完成页面网址跳转
        }else {//失败显示错误提示页面
            req.getRequestDispatcher("WEB-INF/error.jsp").forward(req, resp);
        }
    }
}
